/**
 *  列表页
 *
 * 作者 admin
 * 日期 2024-08-19 14:13:44
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardExamine'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardExamine-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'hazardExamineTable',
            url: ctx + 'hazardExamine/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'examineId', title: '主键', hide:true},
                {field: 'tenantName', title: '公司名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'examineNo', title: '检查编号',align:'center', width: 200,event:'detail',templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.examineNo+'</p>'
                    }
                },
                {field: 'examineTitle', title: '检查主题', minWidth: 500},
                {field: 'examineCategory', title: '检查类别',align:'center', width: 120, templet: function (d) {
                        var flg = '';
                        switch (d.examineCategory) {
                            case 1: {
                                flg = '<span class="layui-badge febs-bg-gold">日常</span>'
                                break;
                            }
                            case 2: {
                                flg = '<span class="layui-badge febs-bg-blue">专项</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'examineType', title: '检查类型',align:'center', width: 120, templet: function (d) {
                        var flg = '';
                        switch (d.examineType) {
                            case "1": {
                                flg = '<span>公司检查</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span >部门检查</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span >车间检查</span>'
                                break;
                            }
                            case "4": {
                                flg = '<span >班组检查</span>'
                                break;
                            }
                            case "5": {
                                flg = '<span >项目检查</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'startDate', title: '开始时间',align:'center', width: 120},
                {field: 'endDate', title: '结束时间',align:'center', width: 120},
                {field: 'objectId', title: '项目Id', minWidth: 120,hide:true},
                // {field: 'objectName', title: '项目名称', minWidth: 180},
                // {field: 'areaName', title: '区域位置', minWidth: 180},
                {field: 'status', title: '状态',align:'center', width: 120, templet: function (d) {
                        var flg = '';
                        switch (d.status) {
                            case "1": {
                                flg = '<span class="layui-badge febs-bg-cyan">待发布</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span class="layui-badge febs-bg-orange">待检查</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span class="layui-badge febs-bg-green">进行中</span>'
                                break;
                            }
                            case "4": {
                                flg = '<span class="layui-badge febs-bg-volcano">已结束</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {title: '操作',align:'center', fixed:'right', toolbar: '#hazardExamine-tool', width: 160}
            ]],
            toolbar: '#hazardExamine-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(hazardExamineTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(hazardExamineTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增安全检查', 'hazardExamine/add', {
                btn: ['保存', '提交','取消'],
                area: $(window).width() <= 750 ? ['95%', '85%']: ['80%', '85%'],
                yes: function (index, layero) {
                    $('#hazardExamine-edit').find('#save').trigger('click');
                },
                btn2: function (index, layero) {
                    $('#hazardExamine-edit').find('#submit').trigger('click');
                    return false;
                },
                btn3: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: currentTenantId,
                }
            });
        }
    });

    table.on('tool(hazardExamineTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if(obj.event === 'detail'){
            febs.modal.open('安全检查信息', 'hazardExamine/detail/'+data.examineId, {
                btn: ['关闭'],
                area: $(window).width() <= 750 ?  ['95%', '85%']:['80%', '85%'],
                shadeClose:true,
                yes: function (index, layero) {
                    layer.closeAll();
                },
            });
        }else if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.examineId);
            });
        }else if (layEvent === 'edit') {
            febs.modal.open('修改安全检查', 'hazardExamine/update/' + data.examineId, {
                area: $(window).width() <= 750 ?  ['95%', '85%']:['80%', '85%'],
                btn: ['保存','提交', '取消'],
                yes: function (index, layero) {
                    $('#hazardExamine-edit').find('#save').trigger('click');
                },
                btn2: function (index, layero) {
                    $('#hazardExamine-edit').find('#submit').trigger('click');
                    return false;
                },
                btn3: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: data.tenantId,
                },
            });
        }
    });

    function getQueryParams() {
        return {
            businessType:'examine',
            tenantId: $searchForm.find("select[name='tenantSelect']").find("option:selected").val(),
            examineNo: $searchForm.find('input[name="examineNo"]').val()?.trim(),
            examineTitle: $searchForm.find('input[name="examineTitle"]').val()?.trim(),
            examineType: $searchForm.find("select[name='examineType']").find("option:selected").val(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'hazardExamine/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});